package com.molyfun.parents.push;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.molyfun.parents.activity.MainActivity;
import com.molyfun.parents.utils.GsonUtil;
import com.molyfun.parents.utils.PreferencesStorageUtil;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:1/21/17.
 */

public class JpushReceiver extends BroadcastReceiver {

    private static final String TAG = "TalkReceiver";

    private NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        Bundle bundle = intent.getExtras();
        Log.d(TAG, "onReceive - " + intent.getAction());

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.d(TAG, "JPush用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "接受到推送下来的自定义消息");
            // Push Talk messages are push down by custom message format
//            processCustomMessage(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
            Log.d(TAG, "接受到推送下来的通知");
            PushMsgBody pushMsgBody = getData(bundle);
            Log.d(TAG, "id--->" + pushMsgBody.getId());
            String type = pushMsgBody.getType();
            PushMsgCacheCenter.getInstance().putPushMsg(title, pushMsgBody.getId());
            PushMsgCacheCenter.getInstance().unReadCountPlus();


        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "用户点击打开了通知");
//            PushMsgBody pushMsgBody = getData(bundle);
//
//            PushNoticeActivity.startActivityNewTask(context, pushMsgBody.getId());
            if(PreferencesStorageUtil.isUserEnter(context)){
                Intent toMainIntent = new Intent(context, MainActivity.class);
                toMainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(toMainIntent);
            }
        } else {
            Log.d(TAG, "Unhandled intent - " + intent.getAction());
        }
    }

    private PushMsgBody getData(Bundle bundle){
        PushMsgBody json = GsonUtil.fromJson(bundle.getString(JPushInterface.EXTRA_EXTRA), PushMsgBody.class);
        return json;
    }

    public static class PushMsgBody{
        private String id;
        private String type;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }

}
